package com.maimao.upload.controller;

import com.maimao.api.upload.UploadApi;
import com.maimao.core.constant.ListData;
import com.maimao.core.enums.RespCode;
import com.maimao.core.model.auth.AuthUser;
import com.maimao.core.model.resp.Result;
import com.maimao.upload.controller.base.BaseController;
import com.maimao.upload.service.UploadService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.List;

/**
 * @author MaoLin Wang
 * @date 2020/12/10 4:40 下午
 */
@RestController
public class UploadController extends BaseController implements UploadApi {
    @Autowired
    private UploadService uploadService;

    @PostMapping(value = "/delete/batch")
    @Override
    public void deleteBatch(@RequestBody List<String> urls) {
        if (urls == null || urls.size() <= 0) {
            return;
        }
        uploadService.deleteBatch(urls);
    }

    /**
     * 上传品牌log
     *
     * @param file
     * @return
     */
    @PostMapping(value = "/brand/logo", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @Override
    public Result<?> uploadImage(@RequestParam("brandId") Integer brandiId, @RequestParam("file") MultipartFile file) {
        String url = uploadService.uploadBrand(file, brandiId);

        return Result.okData(RespCode.OK, url);
    }

    @Override
    @PostMapping("/member/id-card")
    public Result<?> uploadMemberIdCard(@RequestParam("type") String type, MultipartFile file) {
        if (StringUtils.isEmpty(type) || !ListData.MEMBER_ID_TYPE.contains(type)) {
            return Result.notValid("类型错误");
        }
        AuthUser authUser = checkAndGetAuthUser();
        String url = uploadService.uploadMemberIdCard(file, type, Long.valueOf(authUser.getId()));

        return Result.okData(RespCode.OK, url);
    }

    /**
     * 通用图片上传
     */
    @Override
    @PostMapping("/image")
    public Result<?> uploadCommon(@RequestParam("file") MultipartFile file, @RequestParam("limit") Integer limit) {
        if (limit == null || limit < 1) {
            return Result.notValid("限制大小非法");
        }
        String url = uploadService.uploadCommon(file, limit, true);
        if (url == null) {
            return Result.notValid("图片大小不能超过" + limit + "MB");
        }
        return Result.okData(RespCode.OK, url);
    }

    /**
     * 通用文件上传
     */
    @Override
    @PostMapping("/file")
    public Result<?> uploadCommonFile(@RequestParam("file") MultipartFile file, @RequestParam("limit") Integer mb) {
        if (mb == null || mb < 1) {
            return Result.notValid("限制大小非法");
        }
        String url = uploadService.uploadCommon(file, mb, false);
        if (url == null) {
            return Result.notValid("文件大小不能超过" + mb + "MB");
        }
        return Result.okData(RespCode.OK, url);
    }

}
